package com.xialing.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xialing.weblog.common.domain.dos.VideoCollectionDO;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author: 夏翎
 * @url: www.xialing.com
 * @date: 2025-09-23
 * @description: 视频合集Mapper
 **/
public interface VideoCollectionMapper extends BaseMapper<VideoCollectionDO> {

    /**
     * 分页查询合集
     * @param current 当前页
     * @param size 每页大小
     * @param name 合集名称
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 分页结果
     */
    default Page<VideoCollectionDO> selectPageList(Long current, Long size, String name, LocalDate startDate, LocalDate endDate) {
        // 分页对象(查询第几页、每页多少数据)
        Page<VideoCollectionDO> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<VideoCollectionDO> wrapper = Wrappers.<VideoCollectionDO>lambdaQuery()
                .like(StringUtils.isNotBlank(name), VideoCollectionDO::getName, name) // like 模糊查询
                .ge(Objects.nonNull(startDate), VideoCollectionDO::getCreateTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), VideoCollectionDO::getCreateTime, endDate)  // 小于等于 endDate
                .eq(VideoCollectionDO::getIsDeleted, false) // 未删除
                .orderByDesc(VideoCollectionDO::getCreateTime); // 按创建时间倒序

        return selectPage(page, wrapper);
    }

    /**
     * 查询所有未删除的合集
     * @return 合集列表
     */
    default List<VideoCollectionDO> selectAllCollections() {
        return selectList(Wrappers.<VideoCollectionDO>lambdaQuery()
                .eq(VideoCollectionDO::getIsDeleted, false) // 未删除
                .orderByDesc(VideoCollectionDO::getCreateTime)); // 按创建时间倒序
    }

    /**
     * 根据合集名称查询
     * @param name 合集名称
     * @return 合集信息
     */
    default VideoCollectionDO selectByName(String name) {
        return selectOne(Wrappers.<VideoCollectionDO>lambdaQuery()
                .eq(VideoCollectionDO::getName, name)
                .eq(VideoCollectionDO::getIsDeleted, false)); // 未删除
    }

    /**
     * 逻辑删除合集
     * @param collectionId 合集ID
     * @return 删除结果
     */
    default int deleteLogically(Long collectionId) {
        return update(null, Wrappers.<VideoCollectionDO>lambdaUpdate()
                .set(VideoCollectionDO::getIsDeleted, true)
                .eq(VideoCollectionDO::getId, collectionId));
    }

    /**
     * 批量逻辑删除合集
     * @param collectionIds 合集ID列表
     * @return 删除结果
     */
    default int deleteLogicallyByIds(List<Long> collectionIds) {
        return update(null, Wrappers.<VideoCollectionDO>lambdaUpdate()
                .set(VideoCollectionDO::getIsDeleted, true)
                .in(VideoCollectionDO::getId, collectionIds));
    }

    /**
     * 更新合集统计信息
     * @param collectionId 合集ID
     * @param totalVideos 视频总数
     * @param totalDuration 总时长
     * @param totalViews 总观看量
     * @return 更新结果
     */
    default int updateStatistics(Long collectionId, Integer totalVideos, Integer totalDuration, Long totalViews) {
        return update(null, Wrappers.<VideoCollectionDO>lambdaUpdate()
                .set(VideoCollectionDO::getTotalVideos, totalVideos)
                .set(VideoCollectionDO::getTotalDuration, totalDuration)
                .set(VideoCollectionDO::getTotalViews, totalViews)
                .eq(VideoCollectionDO::getId, collectionId));
    }
}

